package com.ruoyi.airspaceFlight.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.airspaceFlight.domain.FlyAction;
import com.ruoyi.airspaceFlight.domain.FlyApply;
import com.ruoyi.airspaceFlight.dto.resp.ReleaseApplyAndFlyApplyVo;
import com.ruoyi.airspaceFlight.mapper.FlyActionMapper;
import com.ruoyi.airspaceFlight.mapper.FlyApplyMapper;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.airspaceFlight.mapper.ReleaseFlyApplyMapper;
import com.ruoyi.airspaceFlight.domain.ReleaseFlyApply;
import com.ruoyi.airspaceFlight.service.IReleaseFlyApplyService;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 放飞申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-06
 */
@Service
public class ReleaseFlyApplyServiceImpl implements IReleaseFlyApplyService 
{
    @Autowired
    private ReleaseFlyApplyMapper releaseFlyApplyMapper;

    @Autowired
    private FlyApplyMapper flyApplyMapper;

    @Autowired
    private FlyActionMapper flyActionMapper;
    /**
     * 查询放飞申请
     * 
     * @param releaseApplyId 放飞申请主键
     * @return 放飞申请
     */
    @Override
    public ReleaseFlyApply selectReleaseFlyApplyByReleaseApplyId(String releaseApplyId)
    {
        return releaseFlyApplyMapper.selectReleaseFlyApplyByReleaseApplyId(releaseApplyId);
    }

    /**
     * 查询放飞申请列表
     * 
     * @param releaseFlyApply 放飞申请
     * @return 放飞申请
     */
    @Override
    public List<ReleaseApplyAndFlyApplyVo> selectReleaseFlyApplyList( ReleaseFlyApply releaseFlyApply)
    {
        ArrayList<ReleaseApplyAndFlyApplyVo> releaseApplyAndFlyApplyVos = new ArrayList<>();
        List<ReleaseFlyApply> releaseFlyApplies = releaseFlyApplyMapper.selectReleaseFlyApplyList(releaseFlyApply);
        for (ReleaseFlyApply flyApply : releaseFlyApplies) {
            FlyApply flyApply1 = flyApplyMapper.selectFlyApplyByFlyApplyId(flyApply.getFlyApplyId());
            ReleaseApplyAndFlyApplyVo releaseApplyAndFlyApplyVo = new ReleaseApplyAndFlyApplyVo(flyApply, flyApply1);
            releaseApplyAndFlyApplyVos.add(releaseApplyAndFlyApplyVo);
        }
        return releaseApplyAndFlyApplyVos;
    }

    /**
     * 新增放飞申请
     * 
     * @param releaseFlyApply 放飞申请
     * @return 结果
     */
    @Override
    public int insertReleaseFlyApply(ReleaseFlyApply releaseFlyApply)
    {
        releaseFlyApply.setCreateTime(DateUtils.getNowDate());
        return releaseFlyApplyMapper.insertReleaseFlyApply(releaseFlyApply);
    }

    /**
     * 修改放飞申请
     * 
     * @param releaseFlyApply 放飞申请
     * @return 结果
     */
    @Override
    public int updateReleaseFlyApply(ReleaseFlyApply releaseFlyApply)
    {
        releaseFlyApply.setUpdateTime(DateUtils.getNowDate());

//        releaseFlyApply.setUpdateBy(SecurityUtils.getUsername());
        int i = releaseFlyApplyMapper.updateReleaseFlyApply(releaseFlyApply);
        ReleaseFlyApply releaseFlyApply1 = releaseFlyApplyMapper.selectReleaseFlyApplyByReleaseApplyId(releaseFlyApply.getReleaseApplyId());
        if ((releaseFlyApply.getReleaseStatus() == 2)) {
            FlyAction flyAction = new FlyAction();
            BeanUtil.copyProperties(releaseFlyApply1,flyAction);
            flyAction.setFlyApplyId(releaseFlyApply1.getFlyApplyId());

            //设置飞行计划编号（根据飞行计划id查找）
            FlyApply flyApply = flyApplyMapper.selectFlyApplyByFlyApplyId(releaseFlyApply1.getFlyApplyId());

            flyAction.setFlyApplyCode(flyApply.getFlyApplyCode());


            //生成飞行实施计划表

            //固定数据
            flyAction.setCreateTime(DateUtils.getNowDate());
            flyAction.setUpdateTime(DateUtils.getNowDate());
            flyAction.setFlyActionId(IdUtils.randomUUID());
//        ReleaseFlyApply.setCreateBy(SecurityUtils.getUsername());
//        ReleaseFlyApply.setUpdateBy(SecurityUtils.getUsername());
            flyActionMapper.insertFlyAction(flyAction);
        }

        return i;
    }

    @Override
    public int updateReleaseFlyApplys(List<ReleaseFlyApply> releaseFlyApplys) {
        int i = 0;
        for (ReleaseFlyApply releaseFlyApply : releaseFlyApplys) {
            releaseFlyApply.setUpdateTime(DateUtils.getNowDate());
            releaseFlyApplyMapper.updateReleaseFlyApply(releaseFlyApply);
            i++;
        }
        return i;
    }

    /**
     * 批量删除放飞申请
     * 
     * @param releaseApplyIds 需要删除的放飞申请主键
     * @return 结果
     */
    @Override
    public int deleteReleaseFlyApplyByReleaseApplyIds(String[] releaseApplyIds)
    {
        return releaseFlyApplyMapper.deleteReleaseFlyApplyByReleaseApplyIds(releaseApplyIds);
    }

    /**
     * 删除放飞申请信息
     * 
     * @param releaseApplyId 放飞申请主键
     * @return 结果
     */
    @Override
    public int deleteReleaseFlyApplyByReleaseApplyId(String releaseApplyId)
    {
        return releaseFlyApplyMapper.deleteReleaseFlyApplyByReleaseApplyId(releaseApplyId);
    }
}
